package com.byt.xmxb.activity;

import android.Manifest;
import android.os.Bundle;
import android.view.WindowManager;

import com.billy.cc.core.component.CC;
import com.byt.base.activity.BaseActivity;
import com.byt.base.preference.SharePerUtils;
import com.byt.base.utils.FileUtil;
import com.byt.base.utils.permission.PermissionReq;
import com.byt.base.utils.permission.ResultListener;
import com.byt.common.BaseColor;
import com.byt.common.Constants;
import com.byt.xmxb.R;
import com.byt.xmxb.databinding.ActivityWelBinding;
import com.byt.xmxb.viewmodel.ProvinceViewModel;

import java.io.File;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;

public class WelActivity extends BaseActivity<ActivityWelBinding, ProvinceViewModel> {
    public static final String INP_PUSH_DATA = "PUSH_DATA";

    private String pushData = "";
    private String Tag = "WelActivity";

    @Override
    protected void onRetryBtnClick() {
    }

    @Override
    protected void initParameters() {
        super.initParameters();
        SetStatusBarColor(BaseColor.expert_color_transparent);
        if (getIntent() != null) {
            pushData = getIntent().getStringExtra("stringType");
        }
        Observable.timer(2000, TimeUnit.MILLISECONDS)
                .subscribe(aLong -> setView());
    }

    @Override
    protected int getLayoutId() {
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_wel;
    }

    @Override
    protected ProvinceViewModel getViewModel() {
        return new ProvinceViewModel();
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    private void postData() {
        if (SharePerUtils.getInstance().getLong(Constants.EXPERTS_ID, 0L) != 0) {
            int complete = SharePerUtils.getInstance().getInt(Constants.COMPLETE, 0);
            if (complete == 0) {
                // 跳完善资料
                CC.obtainBuilder("UserCenter").setActionName("login")
                        .build().call();
            } else {
                Bundle bundle = new Bundle();
                bundle.putString(INP_PUSH_DATA, pushData);
                openActivity(MainActivity.class, bundle);
            }
        } else {
            Bundle bundle = new Bundle();
            bundle.putString(INP_PUSH_DATA, pushData);
            openActivity(MainActivity.class, bundle);
//            CC.obtainBuilder("UserCenter")
//                    .setActionName("login")
//                    .build().call();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        finish();
    }

    private void setView() {
        PermissionReq.with(this)
                .permissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .result(new ResultListener() {
                    @Override
                    public void onGranted() {
                        postData();
                        // 删除日志
                        long logTime = SharePerUtils.getInstance().getLong(Constants.LOG_TIME, 0);
                        FileUtil.deleteDir(new File(FileUtil.getLogDir(mBContext)));
                        if (logTime == 0) {
                            SharePerUtils.getInstance().getLong(Constants.LOG_TIME, System.currentTimeMillis());
                        } else {
                            long time = System.currentTimeMillis() - logTime;
                            if (time > 5 * 24 * 60 * 60 * 1000) {
                                SharePerUtils.getInstance().getLong(Constants.LOG_TIME, System.currentTimeMillis());
                            }
                        }
                    }

                    @Override
                    public void onDenied() {
                        postData();
                    }
                }).
                request();
    }
}
